var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');

var app = getApp();
Page({
  data: {
    id: 0,
    isAuth: 0,
    userInfo: {
      nickName: '点击登录',
      avatarUrl: '/static/images/my.png'
    },
    userApproveInfo: {
      contactName: '',
      contactTel: '',
      contactAddress: '',
      userType: 0,
      companyName: '',
      companyLogo: '',
      adminUsername: '',
      herbInfoList: [],
    },
    userTypes: [{
        value: '0',
        name: '个人',
        checked: true
      },
      {
        value: '1',
        name: '车行'
      },
      
    ],
    logoUrls: [],
    files: [],
    idCard1Files: [],
    idCard2Files: [],
    qualificationFiles: [],
    herbInfoStr: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成
    var that = this;
    that.setData({
      id: options.id,
      isAuth: options.isAuth
    });

    this.getQualificationsDetail();
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      this.setData({
        userInfo: userInfo
      });
    }
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  idCard1PreviewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.idCard1Files // 需要预览的图片http链接列表
    })
  },
  idCard2PreviewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.idCard2Files // 需要预览的图片http链接列表
    })
  },
  qualificationsPreviewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.qualificationFiles // 需要预览的图片http链接列表
    })
  },
  stringFormat(v) {
    if (v == undefined || v == null) {
      return '空'
    }
    return v;
  },
  getQualificationsDetail() {
    wx.showLoading({
      title: '加载中',
    });
    setTimeout(function () {
      wx.hideLoading()
    }, 2000);

    let that = this;
    util.request(api.QualificationsDetail, {
      authId: that.data.id,
      isAuth: that.data.isAuth
    }).then(function (res) {
      if (res.errno === 0) {
        // console.log(res.data)
        let userApproveInfo = res.data.userApproveInfo;
        userApproveInfo.approveTime && (userApproveInfo.approveTime = util.formatTime2(new Date(userApproveInfo.approveTime), "Y-M-D h:m:s"))

        that.setData({
          userApproveInfo: userApproveInfo
        });
        let userType = res.data.userApproveInfo.userType;
        if (userType == '0') {
          let herbInfoList = that.data.userApproveInfo.herbInfoList;
          if (herbInfoList && herbInfoList[0] != null) {
            that.setData({
              herbInfoStr: herbInfoList.map((t) => t.genericName).join('、'),
              // herbInfoStr: herbInfoList.map((t) => `${t.herbName}(规格/${that.stringFormat(t.specifications)}-单位/${that.stringFormat(t.measurementUnit)})`).join('\n'),
            });
          }
        } else {
          let companyLogoStr = that.data.userApproveInfo.companyLogo;
          if (companyLogoStr != null && companyLogoStr != '') {
            that.setData({
              files: companyLogoStr.split(',')
            });
          }
          let certificatesImageStr = that.data.userApproveInfo.certificatesImage;
          if (certificatesImageStr != null && certificatesImageStr != '') {
            that.setData({
              qualificationFiles: certificatesImageStr.split(',')
            });
          }
        }
        let idCard1UrlStr = that.data.userApproveInfo.idCard1Url;
        if (idCard1UrlStr != null && idCard1UrlStr != '') {
          that.setData({
            idCard1Files: idCard1UrlStr.split(',')
          });
        }
        let idCard2UrlStr = that.data.userApproveInfo.idCard2Url;
        if (idCard2UrlStr != null && idCard2UrlStr != '') {
          that.setData({
            idCard2Files: idCard2UrlStr.split(',')
          });
        }
      }
      wx.hideLoading();
    });
  },
})